package com.ruoyi.api.client.service.impl;

import com.ruoyi.api.client.domain.dto.FeedbackDto;
import com.ruoyi.api.client.domain.vo.*;
import com.ruoyi.api.client.service.IClientCmsService;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.service.*;
import com.ruoyi.common.constant.CommonConstants;
import com.ruoyi.common.core.domain.BaseSelect;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.satoken.StpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description  客户端 内容管理模块服务层
 * @Author zouhuu
 * @Date 2022/8/5 14:02
 */
@Service
public class ClientCmsServiceImpl implements IClientCmsService {

    @Autowired
    private ICmsFeedbackService cmsFeedbackService;

    @Autowired
    private ICmsDocumentService documentService;

    @Autowired
    private ICmsHelpService cmsHelpService;

    @Autowired
    private ICmsHelpCategoryService cmsHelpCategoryService;

    @Autowired
    private ICmsArticleCategoryService cmsArticleCategoryService;

    @Autowired
    private ICmsArticleService cmsArticleService;

    @Autowired
    private ICmsMessageService cmsMessageService;

    /**
     * 用户反馈
     * @param feedbackDto
     * @return
     */
    @Override
    public R feedback(FeedbackDto feedbackDto) {
        CmsFeedback cmsFeedback = new CmsFeedback();
        BeanUtils.copyBeanProp(cmsFeedback, feedbackDto);
        if (StringUtils.isNull(feedbackDto.getId())) {
            cmsFeedback.setUid(StpClientUtil.getLoginIdAsLong());
            cmsFeedbackService.insertCmsFeedback(cmsFeedback);
            return R.ok("反馈成功");
        } else {
            cmsFeedbackService.updateCmsFeedback(cmsFeedback);
            return R.ok("修改成功");

        }
    }

    /**
     * 意见反馈列表
     * @return
     */
    @Override
    public R feedbackList() {
        CmsFeedback cmsFeedback = new CmsFeedback();
        cmsFeedback.setUid(StpClientUtil.getLoginIdAsLong());
        List<CmsFeedback> cmsFeedbacks = cmsFeedbackService.selectCmsFeedbackList(cmsFeedback);
        List<CmsFeedbackVo> collect = cmsFeedbacks.stream().map(item -> {
            CmsFeedbackVo cmsFeedbackVo = new CmsFeedbackVo();
            BeanUtils.copyBeanProp(cmsFeedbackVo, item);
            return cmsFeedbackVo;
        }).collect(Collectors.toList());
        return R.ok(collect);
    }

    /**
     * 意见反馈详情
     * @param feedbackId
     * @return
     */
    @Override
    public R feedbackDetail(Long feedbackId) {
        CmsFeedback cmsFeedback = cmsFeedbackService.selectCmsFeedbackById(feedbackId);
        CmsFeedbackVo cmsFeedbackVo = new CmsFeedbackVo();
        BeanUtils.copyBeanProp(cmsFeedbackVo, cmsFeedback);
        return R.ok(cmsFeedbackVo);
    }

    /**
     * 意见反馈删除
     * @param feedbackId
     * @return
     */
    @Override
    public R feedbackDel(Long feedbackId) {
        cmsFeedbackService.deleteCmsFeedbackById(feedbackId);
        return R.ok("删除成功");
    }

    /**
     * 平台文档信息
     * @param docId
     * @return
     */
    @Override
    public R docInfo(Long docId) {
        return R.ok(documentService.selectCmsDocumentById(docId));
    }

    /**
     * 帮助文档分类详情
     * @param categoryId
     * @return
     */
    @Override
    public R helpCategoryDetail(Long categoryId) {
        CmsHelpCategory category = cmsHelpCategoryService.selectCmsHelpCategoryById(categoryId);
        CmsHelpCategoryVo cmsHelpCategoryVo = new CmsHelpCategoryVo();
        BeanUtils.copyBeanProp(cmsHelpCategoryVo, category);
        return R.ok(cmsHelpCategoryVo);
    }

    /**
     * 帮助文档列表
     * @param categoryId
     * @return
     */
    @Override
    public R helpList(Long categoryId) {
        if (StringUtils.isNull(categoryId)) {
            return R.fail("请填写正确的类别ID");
        }
        CmsHelp cmsHelp = new CmsHelp();
        cmsHelp.setCategoryId(categoryId);
        List<CmsHelp> cmsHelps = cmsHelpService.selectCmsHelpList(cmsHelp);
        List<CmsHelpVo> collect = cmsHelps.stream().map(item -> {
            CmsHelpVo cmsHelpVo = new CmsHelpVo();
            BeanUtils.copyBeanProp(cmsHelpVo, item);
            return cmsHelpVo;
        }).collect(Collectors.toList());
        return R.ok(collect);
    }

    /**
     * 帮助文档详情
     * @param helpId
     * @return
     */
    @Override
    public R helpDetail(Long helpId) {
        CmsHelp cmsHelp = cmsHelpService.selectCmsHelpById(helpId);
        CmsHelpVo cmsHelpVo = new CmsHelpVo();
        BeanUtils.copyBeanProp(cmsHelpVo, cmsHelp);
        return R.ok(cmsHelpVo);
    }

    /**
     * 新增帮助文档分类
     * @return
     */
    @Override
    public R articleCategory() {
        List<CmsArticleCategory> cmsArticleCategories = cmsArticleCategoryService.selectCmsArticleCategoryList(new CmsArticleCategory());
        List<CmsArticleCategoryVo> collect = cmsArticleCategories.stream()
                .filter(item -> item.getParentId() != 0)
                .map(item -> new CmsArticleCategoryVo(item.getId(), item.getCategoryName()))
                .collect(Collectors.toList());
        return R.ok(collect);
    }

    /**
     * 文章首页
     * @return
     */
    @Override
    public R articleHome() {
        // 文章分类列表
        List<CmsArticleCategory> categoryList = cmsArticleCategoryService.selectCmsArticleCategoryList(new CmsArticleCategory());
        CmsArticle cmsArticle = new CmsArticle();
        cmsArticle.setIsTop(CommonConstants.DATA_YSENO_YES);
        Map<Long, List<CmsArticle>> articleCategoryMap = cmsArticleService.selectCmsArticleList(cmsArticle).stream().collect(Collectors.groupingBy(CmsArticle::getCategoryId));
        List<CmsArticleHomeVo> artHomeList = categoryList.stream()
                .filter(item -> item.getParentId() != 0)
                .map(category -> {
                    List<CmsArticleVo> artVoList = new ArrayList<>();
                    List<CmsArticle> cmsArticles = articleCategoryMap.get(category.getId());
                    if (StringUtils.isNotNull(cmsArticles)) {
                        artVoList = cmsArticles.stream().map(art -> {
                            CmsArticleVo cmsArticleVo = new CmsArticleVo();
                            BeanUtils.copyBeanProp(cmsArticleVo, art);
                            return cmsArticleVo;
                        }).collect(Collectors.toList());
                    }
                    CmsArticleHomeVo artHomeVo = new CmsArticleHomeVo();
                    artHomeVo.setId(category.getId());
                    artHomeVo.setCategoryName(category.getCategoryName());
                    artHomeVo.setArticleVoList(artVoList);
                return artHomeVo;
        }).collect(Collectors.toList());
        return R.ok(artHomeList);
    }

    /**
     * 获取文章列表
     * @param cmsArticle
     * @return
     */
    @Override
    public List<CmsArticleVo> articleList(CmsArticle cmsArticle) {
        List<CmsArticle> cmsArticles = cmsArticleService.selectCmsArticleList(cmsArticle);
        List<CmsArticleVo> collect = cmsArticles.stream().map(item -> {
            CmsArticleVo cmsArticleVo = new CmsArticleVo();
            BeanUtils.copyBeanProp(cmsArticleVo, item);
            return cmsArticleVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取文章详情
     * @param articleId
     * @return
     */
    @Override
    public R articleDetail(Long articleId) {
        CmsArticleVo cmsArticleVo = new CmsArticleVo();
        CmsArticle cmsArticle = cmsArticleService.selectCmsArticleById(articleId);
        BeanUtils.copyBeanProp(cmsArticleVo, cmsArticle);
        CmsArticle article = new CmsArticle();
        article.setId(cmsArticle.getId());
        article.setArticleHit(cmsArticle.getArticleHit() + 1);
        cmsArticleService.updateCmsArticle(article);
        return R.ok(cmsArticleVo);
    }

    /**
     * 消息列表
     * @return
     */
    @Override
    public R msgList(CmsMessage cmsMessage) {
        cmsMessage.setUid(StpClientUtil.getLoginIdAsLong());
        return R.ok(cmsMessageService.selectCmsMessageList(cmsMessage));
    }

    /**
     * 消息详情
     * @param msgId
     * @return
     */
    @Override
    public R msgDetail(Long msgId) {
        CmsMessage cmsMessage = cmsMessageService.selectCmsMessageById(msgId);
        cmsMessageService.updateCmsMessage(cmsMessage);
        return R.ok( cmsMessage);
    }
}
